package com.xxc.hs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xxc.hs.R;
import com.xxc.hs.bean.TraceInfo;
import com.xxc.hs.fragment.SourceTraceFragment;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class TraceAdapter extends RecyclerView.Adapter<TraceAdapter.TraceViewHolder> {

    private Context context;
    private List<TraceInfo> dataList;
    private OnTraceItemClickListener listener;

    public TraceAdapter(Context context, List<TraceInfo> dataList, OnTraceItemClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TraceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_trace_card, parent, false); // 修正为实际布局文件名
        return new TraceViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull TraceViewHolder holder, int position) {
        if (dataList != null && dataList.size() > position) {
            TraceInfo traceInfo = dataList.get(position);
            holder.nameText.setText(traceInfo.getName());
            holder.responsibleText.setText(traceInfo.getResponsible());

            // 优化图片加载（设置固定尺寸，避免内存抖动）
            int imageResId = context.getResources().getIdentifier(
                    traceInfo.getImagePath(), "drawable", context.getPackageName());
            Glide.with(context)
                    .load(imageResId)
                    .override(200, 200)  // 设置列表项图片尺寸
                    .error(R.drawable.temp) // 添加错误占位图
                    .into(holder.imageView);

            // 优化二维码加载
            int qrCodeResId = context.getResources().getIdentifier(
                    traceInfo.getQrCode(), "drawable", context.getPackageName());
            Glide.with(context)
                    .load(qrCodeResId)
                    .override(150, 150)
                    .error(R.drawable.temp) // 添加错误占位图
                    .into(holder.qrCodeImage);

            // 设置点击事件
            holder.traceButton.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onTraceButtonClicked(traceInfo);
                }
            });

            holder.qrCodeImage.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onQrCodeClicked(traceInfo.getQrCode());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    // 更新数据
    public void updateData(List<TraceInfo> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    // 释放资源
    public void release() {
        if (dataList != null) {
            dataList.clear();
            dataList = null;
        }
        listener = null;
    }

    // ViewHolder类
    static class TraceViewHolder extends RecyclerView.ViewHolder {
        TextView nameText, responsibleText;
        ImageView imageView, qrCodeImage;
        View traceButton;

        public TraceViewHolder(@NonNull View itemView) {
            super(itemView);
            nameText = itemView.findViewById(R.id.trace_name);
            responsibleText = itemView.findViewById(R.id.trace_responsible);
            imageView = itemView.findViewById(R.id.trace_image);
            qrCodeImage = itemView.findViewById(R.id.qr_code_image);
            traceButton = itemView.findViewById(R.id.trace_button);
        }
    }

    // 点击事件接口
    public interface OnTraceItemClickListener {
        void onQrCodeClicked(String qrCode);
        void onTraceButtonClicked(TraceInfo traceInfo);
    }
}